/**这里写一些说明*/
package com.pjedu.nbmallshop.service.imp;

import com.pjedu.nbmallcommon.common.ServiceResultEnum;
import com.pjedu.nbmallcommon.utils.BeanUtil;
import com.pjedu.nbmallcommon.utils.PageResult;
import com.pjedu.nbmallshop.entity.MallGoodsInfo;
import com.pjedu.nbmallshop.mapper.MallGoodsInfoMapper;
import java.util.HashMap;

import com.pjedu.nbmallshop.service.MallGoodsInfoService;
import com.pjedu.nbmallshop.vo.MallSearchGoodsVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MallGoodsInfoServiceImpl implements MallGoodsInfoService {

    @Resource
    private MallGoodsInfoMapper mallGoodsInfoMapper;

    @Override
    public PageResult getMallGoodsPage(HashMap pageUtil) {
        List<MallGoodsInfo> goodsList = mallGoodsInfoMapper.findMallGoodsList(pageUtil);
        int total = mallGoodsInfoMapper.getTotalMallGoods(pageUtil);
        PageResult pageResult = new PageResult(goodsList, total, Integer.valueOf(pageUtil.get("limit").toString()), Integer.valueOf(pageUtil.get("page").toString()));
        return pageResult;
    }

    @Override
    public String saveMallGoods(MallGoodsInfo goods) {
        if (mallGoodsInfoMapper.insertSelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public void batchSaveMallGoods(List<MallGoodsInfo> mallGoodsList) {
        if (!CollectionUtils.isEmpty(mallGoodsList)) {
            mallGoodsInfoMapper.batchInsert(mallGoodsList);
        }
    }

    @Override
    public String updateMallGoods(MallGoodsInfo goods) {
        MallGoodsInfo temp = mallGoodsInfoMapper.selectByPrimaryKey(goods.getGoodsId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        goods.setUpdateTime(new Date());
        if (mallGoodsInfoMapper.updateByPrimaryKeySelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public MallGoodsInfo getMallGoodsById(Long id) {
        return mallGoodsInfoMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public Boolean batchUpdateSellStatus(Long[] ids, int sellStatus) {
        return mallGoodsInfoMapper.batchUpdateSellStatus(ids, sellStatus) > 0;
    }

    @Override
    public PageResult searchMallGoods(HashMap pageUtil) {
        List<MallGoodsInfo> goodsList = mallGoodsInfoMapper.findMallGoodsListBySearch(pageUtil);
        int total = mallGoodsInfoMapper.getTotalMallGoodsBySearch(pageUtil);
        List<MallSearchGoodsVO> mallSearchGoodsVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(goodsList)) {
            mallSearchGoodsVOS = BeanUtil.copyList(goodsList, MallSearchGoodsVO.class);
            for (MallSearchGoodsVO mallSearchGoodsVO : mallSearchGoodsVOS) {
                String goodsName = mallSearchGoodsVO.getGoodsName();
                String goodsIntro = mallSearchGoodsVO.getGoodsIntro();
                // 字符串过长导致文字超出的问题
                if (goodsName.length() > 28) {
                    goodsName = goodsName.substring(0, 28) + "...";
                    mallSearchGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 30) {
                    goodsIntro = goodsIntro.substring(0, 30) + "...";
                    mallSearchGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        PageResult pageResult = new PageResult(mallSearchGoodsVOS, total, Integer.valueOf(pageUtil.get("limit").toString()), Integer.valueOf(pageUtil.get("page").toString()));
        return pageResult;
    }
}
